<template>
	<view>
		<view>
		<view v-for="item of addressList" :key="item.addressid" style="border-bottom: 1rpx solid #ccc;" @click="changeOrder(item)">
			<view>
				{{item.name + ' ' + item.tel }}
			</view>
			<view>
				{{ item.province + ' ' + item.city + item.county  + item.addressDetail }}
					<text @click.stop="deleteAddress(item.addressid)">删除</text>
			</view>
		</view>
		<button @click="addAddress">添加地址</button>
	</view>
	</view>
</template>

<script setup>
	import { ref } from 'vue';
	import { onLoad } from '@dcloudio/uni-app'
	import { deleteAddressFn, getAddressListData } from '../../api/address';
	import { updateOrderAddress } from '../../api/order';
	
	const addressList = ref([])
	const time = ref(0)
	const getList = () => {
		getAddressListData({
			userid: uni.getStorageSync('userid')
		}).then(res => {
			console.log(res.data.data)
			addressList.value = res.data.data
		})
	}
	onLoad((options) => {
		getList()
		time.value = options.time
	})
	const addAddress = () => {
			uni.navigateTo({
				url: "/pages/addAddress/addAddress?time=" + time.value
			})
		}
		const deleteAddress = (addressid) => {
			deleteAddressFn({ addressid}).then(res => {
				getList()
			})
		}
		
		const changeOrder = (item) => {
			console.log(item)
			item.time = time.value
			updateOrderAddress(item).then(() => {
				uni.navigateBack()
			})
		}
</script>

<style>

</style>
